<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Book_Picture extends Controller{

public function action_index()
{
}

public function action_list()
{
	$picture = ORM::factory('picture');
	if ($_GET)
	{
		$booklet_id = arr::get($_GET, 'book_id', -1);
		if ($booklet_id != -1)
		{
			$picture->where('booklet_id', '=', $booklet_id);
		}
	}
	$query_result = $picture->find_all();
	$result = $this->create_picture_list($query_result->as_array());
	echo json_encode($result);
}

public function action_comment()
{
	$picture_comment = ORM::factory('comment_picture');
	if ($_GET)
	{
		$picture_id = arr::get($_GET, 'book_id', -1);
		if ($picture_id != -1)
		{
			$picture_comment->where('picture_id', '=', $picture_id);
		}
	}
	$query_result = $picture_comment->find_all();
	$result = $this->create_picture_comment_list($query_result->as_array());
	echo json_encode($result);
}

protected function create_picture_list($array)
{
	$object = array();
	foreach ( $array as $picture)
	{
		array_push($object,
			array(
				'id'			=> $picture->id,
				'description'	=> $picture->description,
				'image'   		=> $picture->image,
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}

protected function create_picture_comment_list($array)
{
	$object = array();
	foreach ( $array as $picture_comment)
	{
		array_push($object,
			array(
				'id'		   => $picture_comment->id,
				'content'	   => $picture_comment->content,
				'ip'   		   => $picture_comment->ip,
				'create_time'  => $picture_comment->create_time,
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}

}




